#include "cali.h"
#include "ui_cali.h"
#include <QDataStream>

Cali::Cali(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cali)
{
    ui->setupUi(this);
}

Cali::~Cali()
{
    delete ui;
}

void Cali::on_pushButton_4_clicked()
{
    QByteArray sendBa = "@3,CAN,n=8:ID=13000000,";

    quint32 firstCail = ui->lineEdit_14->text().toInt();

    QByteArray tempBa;
    QDataStream packets(&tempBa, QIODevice::ReadWrite);
    packets.setByteOrder(QDataStream::BigEndian);

    packets << firstCail;

    quint8 tp[4];
    packets.device()->seek(0);
    packets >> tp[0] >> tp[1] >> tp[2] >> tp[3];
    tempBa.clear();
    for(int i=0; i < 4; i++)
    {
        tempBa.append(ToStr(tp[i], 16, 2));
    }

    sendBa.append(tempBa);
    sendBa.append("00000000#");

    emit SendSet(sendBa);
}

void Cali::on_pushButton_3_clicked()
{
    QByteArray sendBa = "@3,CAN,n=8:ID=13000001,";

    quint32 secCail = ui->lineEdit_15->text().toInt();

    QByteArray tempBa;
    QDataStream packets(&tempBa, QIODevice::ReadWrite);
    packets.setByteOrder(QDataStream::BigEndian);

    packets << secCail;

    quint8 tp[4];
    packets.device()->seek(0);
    packets >> tp[0] >> tp[1] >> tp[2] >> tp[3];
    tempBa.clear();
    for(int i=0; i < 4; i++)
    {
        tempBa.append(ToStr(tp[i], 16, 2));
    }

    sendBa.append(tempBa);
    sendBa.append("00000000#");

    emit SendSet(sendBa);
}

void Cali::on_pushButton_5_clicked()
{
    emit exit_set();
}

QString Cali::ToStr(int number, int base, int size)
{
    return QString("%1").arg(number, size, base, QChar('0'));
}
